package com.ck.javatestspace.tools.img;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;

/**
 * @author chenghai on 2021/12/23 0023. - 星期四
 * @project java-test-space
 */
public class Html2Img {

    static String html = "<!DOCTYPE html>\n" +
            "<html>\n" +
            "<head>\n" +
            "  <title></title>\n" +
            "</head>\n" +
            "<body>\n" +
            "<table border=\"1\" style=\"border-collapse:collapse\">\n" +
            "<tr><th>Variable_name</th><th>Value</th></tr>\n" +
            "<tr><td>Threads_cached</td><td>197</td></tr>\n" +
            "<tr><td>Threads_connected</td><td>796</td></tr>\n" +
            "<tr><td>Threads_created</td><td>2042</td></tr>\n" +
            "<tr><td>Threads_running</td><td>4</td></tr></table>\n" +
            "</body>\n" +
            "</html>";

    static String url = "https://www.baidu.cn/";

    public static void main(String[] args) throws IOException {
//        JEditorPane jEditorPane = new JEditorPane(new URL(url));
        JEditorPane jEditorPane = new JEditorPane("text/html", html);
        jEditorPane.setSize(500, 500);
        BufferedImage img = new BufferedImage(jEditorPane.getWidth(), jEditorPane.getHeight(), BufferedImage.TYPE_BYTE_GRAY);


        SwingUtilities.paintComponent(img.createGraphics(), jEditorPane,
                new JPanel(), 0, 0, img.getWidth(), img.getHeight());
        ImageIO.write(img, "png", new File("outpout.png"));


    }

}
